<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div @click="indexHome">
        <img
          src="../assets/bjr.png"
          alt=""
        >
        <span>宝家人后台管理系统</span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >
        退出
      </el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div
          class="toggle-button"
          @click="toggleCollapse"
        >
          <i :class="toggerIconClass"/>
        </div>
        <!--侧边栏菜单区域-->
        <transition name="fade">
          <el-menu
            ref="menu"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="isCollapse"
            router
            :default-active="activePath"
            :collapse-transition="true"
          >
            <!--一级菜单-->
            <el-submenu
              :index="item.id+''"
              v-for="item in menulist"
              :key="item.id"
            >
              <!--一级菜单的模板区域-->
              <template slot="title">
                <!--图标-->
                <i :class="iconsObj[item.id]"/>
                <!--文本-->
                <span v-text="item.authName"/>
              </template>
              <!--二级菜单-->
              <el-menu-item
                :index="'/'+subItem.path+''"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState('/'+subItem.path,item.id)"
              >
                <template slot="title">
                  <!--图标-->
                  <i v-if="subItem.path === 'users'" class="iconfont icon-s_yonghuliebiao"/>
                  <i v-else-if="subItem.path === 'roles'" class="iconfont icon-jiaoseliebiao"/>
                  <i v-else-if="subItem.path === 'rights'" class="iconfont icon-quanxianliebiao"/>
                  <i v-else-if="subItem.path === 'goods'" class="iconfont icon-shangpinliebiao"/>
                  <i v-else-if="subItem.path === 'params'" class="iconfont icon-chanpincanshu"/>
                  <i v-else-if="subItem.path === 'categories'" class="iconfont icon-shangpinfenlei"/>
                  <i v-else-if="subItem.path === 'orders'" class="iconfont icon-dingdanliebiao3"/>
                  <i v-else-if="subItem.path === 'reports'" class="iconfont icon-shujubaobiao1"/>
                  <i v-else class="iconfont icon-shangpinfenlei1"/>
                  <!--文本-->
                  <span v-text="subItem.authName"/>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </transition>
      </el-aside>
      <!--右侧内容主体-->
      <el-main>
          <!--路由占位符-->
          <router-view/>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        // eslint-disable-next-line
        //左侧菜单数据
        menulist: [],
        iconsObj: {
          125: 'iconfont icon-users',
          103: 'iconfont icon-tijikongjian',
          101: 'iconfont icon-shangpin',
          102: 'iconfont icon-danju',
          145: 'iconfont icon-baobiao'
        },
        // eslint-disable-next-line
        //是否折叠
        isCollapse: false,
        // eslint-disable-next-line
        //切换折叠图标
        toggerIconClass: 'el-icon-arrow-left',
        // eslint-disable-next-line
        //被激活的链接地址
        activePath: ''
      }
    },
    name: 'Home',
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        window.sessionStorage.clear()
        this.$router.push('/admin/login')
        this.$message.success('退出成功!')
      },
      // eslint-disable-next-line
      //获取所有的菜单
      async getMenuList() {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.menulist = res.data
      },
      // eslint-disable-next-line
      //点击按钮，切换菜单的折叠
      toggleCollapse() {
        if (!this.isCollapse) {
          this.toggerIconClass = 'el-icon-arrow-right'
        } else {
          this.toggerIconClass = 'el-icon-arrow-left'
        }
        this.isCollapse = !this.isCollapse
      },
      // eslint-disable-next-line
      //点击logo跳转首页
      indexHome() {
        const id = window.sessionStorage.getItem('menulistId')
        this.$refs.menu.close(id)
        this.$router.push('/welcome')
      },
      // eslint-disable-next-line
      //保存链接的激活状态
      saveNavState(activePath, menulistId) {
        window.sessionStorage.setItem('menulistId', menulistId)
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    img {
      width: 60px;
      height: 60px;
    }
    > div {
      display: flex;
      align-items: center;
      cursor: pointer;
      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont {
    margin-right: 10px;
  }

  .toggle-button {
    background-color: #4A5064;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
    i {
      font-size: 30px;
      color: #fff;
    }
  }

  .fade-enter-active, .fade-leave-active {
    transition: opacity 1.5s;
  }

  .fade-enter, .fade-leave-to {
    opacity: 0;
  }

  .icoTogger-leave-active, .mybox-enter-active {
    transition: all 1s ease;
  }

  .icoTogger-leave-active, .mybox-enter {
    height: 0px !important;
  }

  .icoTogger-leave, .mybox-enter-active {
    height: 500px;
  }
</style>
